package roborally.Commands;

import java.util.*;

import javax.swing.text.html.HTMLDocument.Iterator;

import roborally.*;

/**
 * A class of Pickup-and-use-commands for playing the roborally game.
 * 
 * @version  2.90
 * @author   Michiel Ruelens & Dieter Naten
 */
public class Pickup_and_use extends Command {
	
	/**
	 * Create a new pickup-and-use command.
	 */
	public Pickup_and_use(){
		this.setStringForm("pickup-and-use");
	}

	/**
	 * This command will make the robot pick up any carryable that is on the same position on the same board as the robot.
	 * If multiple carryables exist, one is randomly chosen.
	 */
	@Override
	public void execute(Robot robot) {
		Board board = robot.getBoard();
		Set<Placeable> placeableSet = board.getAllObjectsByPosition(robot.getX(),robot.getY());
		Carryable carryableToPickUpAndUse = null;
		boolean found = false;
		if(placeableSet.size()>=2){
			int i = 0;
			java.util.Iterator<Placeable> it = placeableSet.iterator();
			while(it.hasNext() && found==false){
				Placeable p = it.next();
				if(p instanceof Carryable){
					carryableToPickUpAndUse = (Carryable) p;
					found = true;
				}
			}
		}
		if(found){
			robot.pickUp(carryableToPickUpAndUse);
			robot.useCarryable(carryableToPickUpAndUse);
		}
	}

}
